import { Table } from 'antd';
import Cls from 'classnames';
import { useContext, useEffect, useMemo } from 'react';
import { AssetListManageContext, openAssetColumns, openImportantAssetColumns, openKindAssetColumns, openImportColumns } from '../mconfig';
import { IAssetListActionEnum } from './useManageReducer';

export const UpdateAssetView: React.FC<any> = ({ pageType, openAssetModalStatus, openUploadModalStatus, changeType, setShowInfoDrawer }) => {
  /** 上下文传参 */
  const { selectAssetList, assetListDispatch, curRes, detailObj } = useContext(AssetListManageContext);

  /** 删除组合资产中的某项 */
  const delSelectRow = (record: Record<string, any>) => {
    let idx = selectAssetList.findIndex((item: Record<string, any>) => {
      return item.id === record.id;
    });
    if (idx !== -1) {
      let arr = JSON.parse(JSON.stringify(selectAssetList));
      arr.splice(idx, 1);
      assetListDispatch({
        type: IAssetListActionEnum.SELECT_ASSET,
        payload: arr,
      });
    }
  };

  const openDraw = (record) => {
    setShowInfoDrawer({
      isShowDrawer: true,
      showInfoObj: {
        ...record,
      },
    });
  };

  /** 表格行配置 */
  const columns: Record<string, any>[] = useMemo(() => {
    if (changeType == 0) return openAssetColumns(delSelectRow, openAssetModalStatus, openUploadModalStatus, openDraw, pageType);
    if (changeType == 1) return openImportColumns(delSelectRow, openAssetModalStatus, openUploadModalStatus, openDraw, pageType);
    // if (changeType == 2) return openKindAssetColumns(delSelectRow, openAssetModalStatus, openUploadModalStatus, openDraw, pageType);
    return [];
  }, [selectAssetList, changeType]);

  useEffect(() => {
    if (changeType != detailObj?.changeType) {
      assetListDispatch({
        type: IAssetListActionEnum.SELECT_ASSET,
        payload: [],
      });
    }
  }, [detailObj, changeType]);

  return (
    <div className={Cls('list-table', 'descriptionsType-tableSty')} style={{ marginTop: 10 }}>
      <Table bordered size="small" dataSource={selectAssetList} columns={columns} rowKey={(res) => res?.id} scroll={{ x: 1200, y: '' }} pagination={false} />
    </div>
  );
};
